package com.caigou.modules.gallery.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.caigou.comm.base.bean.CommentBean
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.gallery.bean.GalleryBean
import com.caigou.modules.gallery.bean.GalleryDetailBean
import com.caigou.modules.gallery.bean.GalleryItemBean
import com.caigou.modules.gallery.repository.GalleryRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/12/31
 */
@HiltViewModel
class GalleryViewModel @Inject constructor(private val repository: GalleryRepository) : ViewModel() {

    val galleryListLiveData = MutableLiveData<Resource<MutableList<GalleryBean>>>()
    val collectListLiveData = MutableLiveData<Resource<MutableList<GalleryBean>>>()
    val galleryDetailLiveData = MutableLiveData<Resource<GalleryDetailBean>>()
    val periodListLiveData = MutableLiveData<Resource<MutableList<String>>>()
    val commentListLiveData = MutableLiveData<Resource<CommentBean>>()
    val collectLiveData = MutableLiveData<Resource<String>>()

    fun getGalleryList(type: Int) {
        galleryListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getGalleryList(type)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<MutableList<GalleryBean>>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    galleryListLiveData.value = it
                }
        }
    }

    fun getCollectList(type: Int, userId: String) {
        collectListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getCollectList(type, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<MutableList<GalleryItemBean>>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    collectListLiveData.value = it
                }
        }
    }

    fun getPeriodList(year: Int) {
        periodListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getPeriodList(year)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<MutableList<String>>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    periodListLiveData.value = it
                }
        }
    }

    fun getGalleryDetail(depotId: Int, period: Int, type: Int, userId: String) {
        galleryDetailLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getGalleryDetail(depotId, period, type, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<GalleryDetailBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    galleryDetailLiveData.value = it
                }
        }
    }


    fun getCommentList(page: Int, limit: Int, depotId: Int, dataId: Int, type: Int, sort: Int, userId: String) {
        commentListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getCommentList(page, limit, depotId, dataId, type, sort, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<CommentBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    commentListLiveData.value = it
                }
        }
    }

    fun collect(type: Int, depotId: Int, userId: String) {
        collectLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.collect(type, depotId, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    collectLiveData.value = it
                }
        }
    }

}